package com.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import transformFactory.CaculatePregnentWeek;

import com.Entity.Club;
import com.Entity.Userdata;
import com.Entity.Users;
import com.Service.ClubService;
import com.Service.UserdataService;
import com.Service.UsersService;

public class LoginAction {
	
	private static final Logger logger = Logger.getLogger(LoginAction.class);

	private static String result="erro";
	private String msg="";
	private int flag=0;
	private List<Users> userlist=null;
	
	@Autowired
	private UsersService usersservice;	
	@Autowired
	private UserdataService userdataservice;
	@Autowired
	private ClubService clubService;
	
	Users loginbean=new Users();
	Userdata userdata=new Userdata();
	List<Club> clublist=new ArrayList<Club>();
	List<Club> clubhoslist=new ArrayList<Club>();
	
	HttpSession session = ServletActionContext.getRequest().getSession();
	
	public Users getLoginbean() {
		return loginbean;
	}
	public void setLoginbean(Users loginbean) {
		this.loginbean = loginbean;
	}

	public Userdata getUserdata() {
		return userdata;
	}
	public void setUserdata(Userdata userdata) {
		this.userdata = userdata;
	}
	
	
	public String Login() throws Exception {
		/**
		 * 已经登录过的用户		 * 
		 */
			if(session.getAttribute("temploginbean")!=null){
				Users user=(Users) session.getAttribute("temploginbean");
				switch(user.getUserType()){
				case 1://孕妇
					//初始化同龄圈数据
					clublist=this.clubService.findSameYearClub();
					//初始化医院圈数据
					clubhoslist=this.clubService.findHosClub();
					result="pregnent";
					break;
				case 2:
					result="communityhos";//社区医院
					break;
				case 3:
					result="centerhos";//中心医院
					break;
				case 4:
					result="admin";//管理员账户
					break;
				default:
					result="fail";;//未知角色
				}
				logger.info(result);
			}
			else{
				userlist=this.usersservice.CheckLogin(loginbean.getUserName(),loginbean.getPassword());
				if(userlist!=null&&!userlist.isEmpty()){					
					session.setAttribute("temploginbean", userlist.get(0));	
					session.setAttribute("UserData", this.userdataservice.getById(userlist.get(0).getUserId()));
					switch(userlist.get(0).getUserType()){
					case 1:
						result="pregnent";//孕妇	
						/*
						 * 将孕周数和天数存进session
						 */
						userdata=this.userdataservice.getById(userlist.get(0).getUserId());
						Date myGesDate=userdata.getPregnancyDate();
						int myGesweek=CaculatePregnentWeek.CaculateWeek(myGesDate);
						int myGesday=CaculatePregnentWeek.CaculateRestdays(myGesDate);
						session.setAttribute("myGesweek", myGesweek);
						session.setAttribute("myGesday", myGesday);
						//初始化同龄圈数据
						clublist=this.clubService.findSameYearClub();
						//初始化医院圈数据
						clubhoslist=this.clubService.findHosClub();
						break;
					case 2:
						result="communityhos";//社区医院
						break;
					case 3:
						result="centerhos";//中心医院
						break;
					case 4:
						result="admin";//管理员账户
						break;
					default:
						result="fail";;//未知角色
					}			
				}
				else{
					msg="用户名或密码错误！";
					result="fail";
				}
			}		
			return result;
	}
	
	//logout
	public String Logout() throws Exception{
		if(session!=null){
			session.invalidate();
		}
		msg="您已经成功注销！";	
		return "logoutSucc";
	}
	
	/*
	 * 验证码
	 */
	public void checkCode() throws IOException{

		String inputcode=ServletActionContext.getRequest().getParameter("inputcode");
		String randcode = (String) session.getAttribute("rand");
		System.out.println("randcode:"+randcode);
		System.out.println("inputcode:"+inputcode);
		HttpServletResponse res = ServletActionContext.getResponse();
		res.setContentType("text/xml;charset=utf-8"); 
		PrintWriter out = res.getWriter();
		if(randcode.equals(inputcode))
		{
			flag=1;
		}
		else
		{
			flag=-1;
		}
		out.println(flag);
		out.flush();
		out.close();
	}

	
	
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public List<Club> getClublist() {
		return clublist;
	}
	public void setClublist(List<Club> clublist) {
		this.clublist = clublist;
	}
	public List<Club> getClubhoslist() {
		return clubhoslist;
	}
	public void setClubhoslist(List<Club> clubhoslist) {
		this.clubhoslist = clubhoslist;
	}
	
}
